package com.laxia.exception;


import com.laxia.config.SpringContextHolder;
import com.laxia.model.Result;
import com.laxia.util.StringUtil;
import org.slf4j.helpers.MessageFormatter;

public class ExceptionFactory {

    private static ExceptionDefinitions exceptionDefinitions;

    static {
        exceptionDefinitions = SpringContextHolder.getBean(ExceptionDefinitions.class);
    }


    public static BusinessException create(String errMsg, Object... args) {
        int errorCode = Result.DEFAULT_ERROR_CODE;
        String message = errMsg;

        if (StringUtil.isNumber(errMsg)) {
            errorCode = Integer.parseInt(errMsg);
            message = exceptionDefinitions.getProperty(errMsg);
        }

        message = MessageFormatter.arrayFormat(message, args).getMessage();
        return new BusinessException(errorCode, message);
    }
}
